package com.jh.paymentchannel.business.impl;

import com.jh.paymentchannel.pojo.RealNameAuthRoute;
import com.jh.paymentchannel.pojo.RealNameImg;
import net.sf.json.JSONObject;
import com.jh.paymentchannel.business.RealnameImgBusiness;
import com.jh.paymentchannel.repository.RealnameAuthRouteRepository;
import com.jh.paymentchannel.repository.RealnameImgRepository;
import com.jh.paymentchannel.util.PaymentChannelConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class RealnameImgBusinessImpl implements RealnameImgBusiness {
    @Autowired
    private RealnameImgRepository realnameImgRepository;

    @Autowired
    private RealnameAuthRouteRepository realNameAuthRouterepository;

    @Autowired
    private EntityManager em;

    @Override
    public RealNameImg findRealNamesImgByUserid(long userId) {

        List<RealNameImg> resultList = realnameImgRepository.findRealNameImgByUserid(userId);
        if (resultList.size() > 0) {
            resultList.get(0);
        }
        return new RealNameImg();
    }

    @Override
    public HashMap<Object, Object> findResultByUserid(long userId) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<RealNameImg> resultList = realnameImgRepository.findByUserid(userId);
        for (RealNameImg list : resultList) {


            if (list.getResult() != null && "0".equals(list.getResult())) {
                hashMap.put("success", "0");
                hashMap.put("realname", list.getRealname());
                hashMap.put("idcard", list.getIdcard());
            } else {
                hashMap.put("error", "1");
            }
        }

        return hashMap;
    }

    @Transactional
    @Override
    public Object realNameImg(long userId, String side, String image) {
        //查看调用谁的第三方
        RealNameAuthRoute realNameAuthRoute = realNameAuthRouterepository.getCurActiveRealNameAuthChannel();
        RealNameImg realNameImg = new RealNameImg();
        String realnameChannel = realNameAuthRoute.getCurChannel();
        HashMap<Object, Object> map = new HashMap<>();
        String result = "";

        if (realnameChannel.equalsIgnoreCase(PaymentChannelConstants.REALNAME_CHANNEL_1)) {
//            result = JuHeAPIRealnameAuthService.realNameImg(image, side);
        }
        JSONObject jsonObject = JSONObject.fromObject(result);
        JSONObject resultObject = jsonObject.getJSONObject("result");
        if (resultObject == null || resultObject.isNullObject()) {
            resultObject = new JSONObject();
        }
        if ("0".equals(jsonObject.getString("error_code"))) {

            if (resultObject.getString("realname") != null && resultObject.getString("idcard") != null
                    && resultObject.getString("address") != null) {
                String realname = resultObject.getString("realname");
                realNameImg.setRealname(realname);
                realNameImg.setIdcard(resultObject.getString("idcard"));
                realNameImg.setOrderid(resultObject.getString("orderid"));
                realNameImg.setAddress(resultObject.getString("address"));
                realNameImg.setSide(resultObject.getString("side"));
                realNameImg.setAuthTime(new Date());
                realNameImg.setResult("0");//成功

            }
        } else {
            realNameImg.setResult("1");//失败
            realNameImg.setAuthTime(new Date());

        }
        realNameImg.setUserId(userId);
        //保存数据
        RealNameImg nameImg = realnameImgRepository.save(realNameImg);
        em.flush();
        return nameImg;
    }
}
